package com.bestv.search.common.dao.hibernate;

import com.bestv.search.common.dao.ManualBindDao;
import com.bestv.search.common.model.ManualBind;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.List;

public class ManualBindDaoHibernate extends GenericDaoHibernate<ManualBind, Long> implements ManualBindDao {

    public ManualBindDaoHibernate() {
        super(ManualBind.class);
    }

    @SuppressWarnings("unchecked")
    public ManualBind getManualBindByName(String name) {
        // TODO Auto-generated method stub
        List<ManualBind> list = getHibernateTemplate().find("from ManualBind mb where mb.name = ?", name);
        if (list == null || list.size() == 0)
            return null;
        return list.get(0);
    }

    public int getManualBindCount() {
        return (Integer) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Query nativeSqlQuery = session.createSQLQuery("select count(*) from manual_bind");
                Object result = nativeSqlQuery.uniqueResult();
                if (result == null)
                    return 0;
                log.debug("site count :" + ((BigInteger) result).intValue());
                return ((BigInteger) result).intValue();
            }
        });
    }

    public int getOwnsCount() {
        return (Integer) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Query nativeSqlQuery = session
                        .createSQLQuery("select count(mb.id) from manual_bind as mb inner join site on mb.owner_id = site.id and site.code = 'HDQ'");
                Object result = nativeSqlQuery.uniqueResult();
                if (result == null)
                    return 0;
                return ((BigInteger) result).intValue();
            }
        });
    }

    @SuppressWarnings("unchecked")
    public List<ManualBind> getManualBindsPaging(final int start, final int limit) {
        return (List<ManualBind>) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(final Session session) throws HibernateException, SQLException {
                Query q = session.createQuery("from ManualBind");
                q.setFirstResult(start).setMaxResults(limit);
                return q.list();
            }
        });
    }

    public int updateId(final long oldId, final long newId) {
        return (Integer) getHibernateTemplate().execute(new HibernateCallback() {
            public Integer doInHibernate(Session session) throws HibernateException, SQLException {
                String hql = "update ManualBind mb set mb.id=? where mb.id=?";
                Query query = session.createQuery(hql);
                query.setLong(0, newId);
                query.setLong(1, oldId);
                return query.executeUpdate();
            }

        });
    }

    @SuppressWarnings("unchecked")
    public List<ManualBind> getOwns(final int start, final int limit) {
        return (List<ManualBind>) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(final Session session) throws HibernateException, SQLException {
                Query q = session.createQuery("from ManualBind mb where mb.owner.code='HDQ'");
                if (start >= 0 && limit > 0) {
                    q.setFirstResult(start);
                    q.setMaxResults(limit);
                }
                return q.list();
            }
        });
    }

    @SuppressWarnings("unchecked")
    public List<ManualBind> getFailedList(final String siteCode, final int start, final int limit) {
        return (List<ManualBind>) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                String hql = "from ManualBind mb where mb.failedSitesCode like '%" + siteCode + "%'";
                Query q = session.createQuery(hql);
                if (start >= 0 && limit > 0) {
                    q.setFirstResult(start);
                    q.setMaxResults(limit);
                }
                return q.list();
            }
        });
    }

    public int getFailedCount(final String siteCode) {
        return (Integer) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Query nativeSqlQuery = session
                        .createSQLQuery("select count(id) from manual_bind as mb where mb.failed_sites_code like '%"
                                + siteCode + "%'");
                Object result = nativeSqlQuery.uniqueResult();
                if (result == null)
                    return 0;
                log.debug("failed count :" + ((BigInteger) result).intValue());
                return ((BigInteger) result).intValue();
            }
        });
    }
}
